package com.sdkx.app.gps;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class GpsManager implements LocationListener {
    private static GpsManager instance = new GpsManager();
    private GpsManager(){}
    LocationManager manager;
    ArrayList<WeakReference<LocationListener>> locationlist = new ArrayList<>();



    Location mLocation;
    public static GpsManager getInstance(){
        return instance;
    }
    public void startGps(Context context){
        if(context == null || manager != null) return;
        manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        this.manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 2, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        for(WeakReference<LocationListener> item : locationlist){
            if(item.get() != null){
                item.get().onLocationChanged(location);
            }
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        for(WeakReference<LocationListener> item : locationlist){
            if(item.get() != null){
                item.get().onStatusChanged(provider,status,extras);
            }
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        for(WeakReference<LocationListener> item : locationlist){
            if(item.get() != null){
                item.get().onProviderEnabled(provider);
            }
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        for(WeakReference<LocationListener> item : locationlist){
            if(item.get() != null){
                item.get().onProviderDisabled(provider);
            }
        }
    }

    public void addLocationListener(LocationListener listener){
        locationlist.add(new WeakReference<LocationListener>(listener));
    }
    public void removeLocationListener(LocationListener listener){
        if(listener == null)return;
        for(WeakReference<LocationListener> item : locationlist){
            if(item != null && item.get() != null){
                if(item.equals(listener)){
                    locationlist.remove(item);
                    break;
                }
            }
        }
    }
    public Location getLocation() {
        return mLocation;
    }
}
